$(".readed").click(function () {
        var _attr = $(this).attr("_attr");
        if (_attr == 1) {
            $(this).attr({"src": "/static/index/img/noSelecetedImg.png", "_attr": "0"});
            $(".checkout-footer-r").css("display", "none");
            $(".noPay").css("display", "block");
        } else {
            $(this).attr({"src": "/static/index/img/selectedImg.png", "_attr": "1"});
            $(".checkout-footer-r").css("display", "block");
            $(".noPay").css("display", "none");
        }
    });
//获得地址缓存
    var referrer = document.referrer;
    var addDatasKey = JSON.parse(storage.getItem('addDatasKey'));
    if (referrer.indexOf("add_list") != -1 && addDatasKey) {
        $(".main-1-name").text(addDatasKey.name)
        $(".main-1-tell").text(addDatasKey.tell)
        $(".main-1-add").text(addDatasKey.add)
    }
    storage.removeItem('referrer');
    var cartId = storage.getItem('cartId');
    console.log(cartId);
    if (!cartId) {
        //返回上个页面
        location.href = document.referrer;
    }
    cartId = JSON.parse(cartId);

    $.ajax({
        type: "post",
        url: Url + "checkout",
        data: cartId,
        async: false,
        timeout : 20000,
        beforeSend: function (xhr) {
            //loadData1(defaultData1)

                showDiv()

            xhr.setRequestHeader("token", tokenk);
        },
        success: function (data) {
            hiddenDiv()
            //判断是否有收获没有去添加
            console.log(data);
            if (data.address) {
                setDatas(data)
                var address = JSON.stringify(data.address);
                //设置收获地址缓存
                storage.setItem('user_adrr', address);
                var productsInfo = [];
                for (var i = 0; i < data.data.length; i++) {
                    productsInfo.push({
                        num: data.data[i].num,
                        cart_id: data.data[i].cart_id,
                        product_id: data.data[i].products_id,
                    });
                }
                productsInfo = JSON.stringify(productsInfo);
                storage.setItem('productsInfo', productsInfo);
            } else {
                location.href = 'add.html';
            }
        },
        complete: function () {

        },
    });

    function setDatas(data) {
        $(".greater").find("s").text(data.priceAll);
        $(".checkout-footer-l").find("s").text(data.priceAll);
        $(".main-1-name").text(data.address.uaddres_name);
        $(".main-1-tell").text(data.address.uaddres_mobile);
        $(".main-1-add").text(data.address.uaddres_province + data.address.uaddres_city + data.address.uaddres_county + data.address.uaddres_detail);
        var str = '';
        for (var i = 0; i < data.data.length; i++) {
            var str1 = '';
            for (var j in data.data[i].snapshot) {
                str1 += '<span> ' + j + ':' + data.data[i].snapshot[j] + '</span>'
            }
            str += '<div class="checkout-main clear" >' +
                '<img  class="checkout-main-img" src="' + data.data[i].pspeci_img + '"/>' +
                '<div class="checkout-main-title">' +
                '<div class="checkout-main-title-t clear">' +
                '<span class="cmt-name">' + data.data[i].products_name + '</span>' +
                '<span class="cmt-num">x ' + data.data[i].num + '</span>' +
                '</div>' +
                '<div class="checkout-main-varieties">' +
                str1 +
                '</div>' +
                '<div class="checkout-main-price">' +
                '￥' + data.data[i].pspeci_price +
                '</div>' +
                '</div>' +
                '</div>'
        }
        $(".checkout-main-main").html(str)
    }

    $("#confirmOrder").on('click', function () {
        //判断收货地址是否有
        var addr = storage.getItem('user_adrr');
        if (!addr) {
            location.href = "checkout.html";
        }
        var productsInfo = storage.getItem('productsInfo');
        console.log(productsInfo);
        if (productsInfo.length < 0) {
            location.href = "checkout.html";
        }
        //console.log(productsInfo);
        var OrderInfo = {};
        OrderInfo['productsInfo'] = JSON.parse(productsInfo);
        OrderInfo['addr'] = JSON.parse(addr);
        console.log(OrderInfo);
        //去创建订单
        $.ajax({
            type: "post",
            url: Url + "order",
            data: OrderInfo,
            async: false,
            timeout : 20000,
            beforeSend: function (xhr) {
                //loadData1(defaultData1)

                    showDiv()

                xhr.setRequestHeader("token", tokenk);
            },
            success: function (data) {
                hiddenDiv()
                console.log(data);
                //判断是否有收获没有去添加
                if (data.pass) {
                    //更新订单状态
                    var id = data.order_no;
                    //开始支付
                    _execPay(id);
                } else {
                    //that._orderFail(data);  // 下单失败
                    alert('下单失败');
                }
            },
            complete: function () {
            },
        });

    })

    function _execPay(id) {
        $.ajax({
            type: "post",
            url: Url + "pay/pre_order",
            data: {order_no: id},
            async: false,
            timeout : 20000,
            beforeSend: function (xhr) {
                //loadData1(defaultData1)

                    showDiv()

                xhr.setRequestHeader("token", tokenk);
            },
            success: function (data) {
                hiddenDiv()
                var datas = JSON.stringify(data);
                var token_key = randomString(12);
                storage.setItem(token_key, datas);
                location.href = "http://admin.chinlingcloud.com/index/index/go_pay.html?id=" + token_key;
                return false;
            },
            error: function (request, status, err) {
                failure(status)
            },
            complete: function () {
            },
        });
    }
